<style scoped>
.wraper {
    padding: 0 30px;
}

.search {
    padding-bottom: 20px;
}

.search span {
    color: #68777c;
    font-weight: bold;
    padding-right: 18px;
}

.search button {
    background-color: #00a8f7;
    color: #fff;
    margin-left: 8px;
}

.top {
    margin-bottom: 20px;
    border-top: 1px dashed #cccccc;
}

.detail {
    border-top: 1px dashed #cccccc;
}

.wraper .headline {
    position: relative;
    font-size: 12px;
    color: #303134;
    font-weight: bold;
    margin: 10px 0;
    line-height: 32px;
    height: 32px;
}

.wraper .add-agent {
    position: absolute;
    top: 0px;
    right: 30px;
    background-color: #348bf2;
    color: #fff;
}

.iconfont {
    font-size: 14px;
}

.red-font {
    color: #ff1f1f;
}

/* 分页 start */

.page {
    margin: 20px 0;
}
.ivu-page {
    padding-bottom: 150px;
} 

/* 分页 end */

.fixed-bottom{
    padding-left: 20px;
    padding-right: 20px;
    height: 80px;
    line-height: normal !important;
    position: fixed;
    left: 210px;
    right: 0;
    bottom: 0;
    z-index: 999;
    text-align: center;
    background-color: rgb(255, 255, 255);
    box-shadow: 0px 0px 9px 0px rgba(0, 0, 0, 0.28);
}
.sidehide{
    left: 75px;
}

</style>

<template>
    <div class="employee-manage">
        <product-title></product-title>
        <div class="wraper">
            <div class="detail">
                <div class="headline">
                    <Button class="add-agent" v-if="$isShow('yun_dept/create')" @click='showAdd(-1)'>新增部门</Button>
                </div>
                <div class="clearfix">
                    <div class="table-theme highlight_table">
                        <Table :loading="tableloading" ref="currentRowTable" size="large" @on-current-change="tableCurrentChange($event)" stripe highlight-row border :columns="columns_dept" :data="data_dept"></Table>
                    </div>
                </div>
            </div>
        </div>
        <Modal v-model="showAddModal" :mask-closable='closeable' width="600" title="添加/编辑部门" @on-ok="add" :loading="loading">
            <Row>
                <div class="common-box-body">
                    <Col span="18" offset="3">
                    <Form :model="dept" :ref="dept" :rules="ruleValidate" :label-width="120">
                        <Form-item label="部门名称：" prop="name" required>
                            <Input v-model="dept.name" placeholder="请输入部门名称"></Input>
                        </Form-item> 
                        <Form-item label="部门别称：">
                            <Input v-model="dept.aname" placeholder="请输入部门别称"></Input>
                        </Form-item> 
                        <Form-item label="上级部门：" prop="pid" required>
                            <Select v-model="dept.pid">
                                <Option :value="zero">无</Option>                            
                                <Option :value="item.id" v-for="item in option_dept" v-if="item.id != dept.id" :key="item.id">{{item.name}}</Option>                        
                            </Select>
                        </Form-item>                              
                    </Form>
                    </Col>
                </div>
            </Row>
        </Modal>
        <div :class="slidetoggle?'fixed-bottom':'fixed-bottom sidehide' ">
            <Row class="pro-pagesize">
                <Col :md="24" :lg="16">
                    <Button v-if="$isShow('yun_dept/del')" type="error" :disabled="tableaction" @click="tableDel" size="large" class="fl mr10 ">删除</Button>
                    <Button v-if="$isShow('yun_dept/update')" type="primary" :disabled="tableaction" @click="tableEdit" size="large" class="fl mr10">编辑</Button>
                </Col>
            </Row>
        </div>
    </div>
</template>

<script>
import productTitle from '../../components/productTitle'
import { saveDept , getDeptList , updateDept ,delDept } from '../../api/dept'
export default {
    data() {
        return {
            zero:0,
            cou:-1,
            checkModal:false,
            showactionModal:false,
            closeable: false,
            loading: true,
            tableloading:true,
            tableaction:true, 
            dept: {
                name: '',
                aname:'',
                pid:0
            },
            currentRow:{},
            showAddModal: false,


            columns_dept: [
                {
                    type: 'index',
                    title:'序号',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '部门名称',
                    key: 'name',
                    align: 'center',
                },
                {
                    title: '部门别称',
                    key: 'aname',
                    align: 'center',
                },
                {
                    title: '上级部门',
                    key: 'p_name',
                    align: 'center',
                },
                {
                    title: '岗位',
                    key: 'post_name',
                    align: 'center',
                }
                // {
                //     title: '操作',
                //     key: 'action',
                //     width: 120,
                //     align: 'center',
                //     render: (h, params) => {
                //         return h('div', [
                //             h('Button', {
                //                 props: {
                //                     type: 'primary',
                //                     size: 'small'
                //                 },
                //                 style: {
                //                     marginRight: '5px'
                //                 },
                //                 on: {
                //                     click: () => {
                //                         this.showAdd(params.index)
                //                     }
                //                 }
                //             }, '编辑'),
                //             h('Button', {
                //                 props: {
                //                     type: 'error',
                //                     size: 'small'
                //                 },
                //                 on: {
                //                     click: () => {
                //                         this.delConfirm(params.index)
                //                     }
                //                 }
                //             }, '删除')
                //         ]);
                //     }
                // },
            ],
            data_dept: [

            ],
            option_dept:[

            ],
            ruleValidate: {
                name: [
                    { required: true, message: '部门名称不能为空', trigger: 'blur' }
                ],
                pid: [
                    { required: true, type:'number', transform(value) {
                    return parseInt(value);
                    }, message: '上级部门不能为空', trigger: 'blur' }
                ],
            },
        }
    },
    components: {
        productTitle
    },
    computed: {
        slidetoggle(){
            return this.$store.state.sildeshow
        },
    },
    created() {
        this.getList()
    },
    methods: {
        tableCurrentChange(currentRow,oldCurrentRow){
            console.log(currentRow)
            console.log(oldCurrentRow)
            this.tableaction = false;
            this.currentRow = currentRow
            console.log(this.currentRow)
        },
        // 搜索
        search() {
            this.getList();
        },
        // 获取职级列表
        getList() {
             this.tableaction = true
            this.tableloading = true
            new Promise((resolve, reject) => {
                getDeptList().then(response => {
                    this.tableloading = false
                    console.log(response.data);
                    if (response.data.err == 0) {
                        this.data_dept = response.data.data;
                        this.$set(this,'option_dept',response.data.data)
                    } else {
                        this.$Message.error(response.data.info);
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },    
        add() {
            this.$refs[this.dept].validate((valid) => {
                if (valid) {
                    if(this.cou == -1){                        
                        new Promise((resolve, reject) => {
                            saveDept(this.dept).then(response => {
                                if (response.data.err == 0) {
                                    this.showAddModal = false;
                                    this.loading = true;
                                    this.getList();
                                    this.$Message.success(response.data.info);
                                } else {
                                    this.$Message.error(response.data.info);
                                    setTimeout(() => {
                                        this.loading = false;
                                        this.$nextTick(() => {
                                            this.loading = true;
                                        });
                                    }, 2000);
                                }
                                resolve();
                            }).catch(error => {
                                setTimeout(() => {
                                    this.loading = false;
                                    this.$nextTick(() => {
                                        this.loading = true;
                                    });
                                }, 2000);
                                reject(error);
                            });
                        });
                    }else{
                        new Promise((resolve, reject) => {
                            updateDept(this.dept).then(response => {
                                if (response.data.err == 0) {
                                    this.showAddModal = false;
                                    this.loading = true;
                                    this.getList();
                                    this.$Message.success(response.data.info);
                                } else {
                                    this.$Message.error(response.data.info);
                                    setTimeout(() => {
                                        this.loading = false;
                                        this.$nextTick(() => {
                                            this.loading = true;
                                        });
                                    }, 2000);
                                }
                                resolve();
                            }).catch(error => {
                                reject(error);
                            });
                        });
                    }                   
                } else {
                    // 通过loading来阻止modal隐藏，然后手动修改loading来充值确定按钮
                    setTimeout(() => {
                        this.loading = false;
                        this.$nextTick(() => {
                            this.loading = true;
                        });
                    }, 2000);
                }
            })
        },
        
        del(param) {
            new Promise((resolve, reject) => {
                delDept(param).then(response => {
                    
                    if (response.data.err == 0) {
                        this.$Message.success(response.data.info)
                        this.getList();
                    }
                    resolve();
                }).catch(error => {
                    reject(error);
                });
            });
        },
        //添加or编辑代理人，打开modal
        showAdd(index) {
            this.showAddModal = !this.showAddModal;
            if (index != -1) {
                this.dept = Object.assign({}, this.currentRow)               
                this.cou = 1
            } else {
                this.dept = {}
                this.dept.name = ""
                this.dept.aname = ""
                this.dept.pid = 0
                this.cou = -1
            }
        },
        //编辑
        tableEdit(){
            console.log(this.currentRow.id)
            this.showAdd(1)
        },
        // 删除时弹出对话框
        tableDel() {
            this.$Modal.confirm({
                title: '提示',
                content: '是否要删除该部门?',
                onOk: () => {
                    let _params = {
                        'id': this.currentRow.id
                    }
                    this.del(_params);
                },
            });
        },
    }
}
</script>
